package action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import bean.Country;
import bean.Destination;
import bean.User;
import bean.UserDestGrading;
import bean.UserDestGradingId;
import dao.CountryDAO;
import dao.DestinationDAO;
import dao.UserDAO;

public class VoteDestinationAction extends Action {
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		UserDAO userDAO=new UserDAO();
		DestinationDAO destDAO=new DestinationDAO();
		CountryDAO countryDAO=new CountryDAO();
		ActionErrors errors=new ActionErrors();
		HttpSession session=request.getSession(false);

		User user=(User)session.getAttribute("user");
		Destination dest=destDAO.findById(Integer.parseInt(request.getParameter("dest")));

		if(request.getParameter("visited").equalsIgnoreCase("y")&&dest!=null){
			//user visited this destination, create userDestination object
			UserDestGradingId userDestId=new UserDestGradingId(user,dest);
			
			session.setAttribute("userDestID", userDestId);
			System.out.print(userDestId.getUser().toString()+userDestId.getDestination().toString());
			request.setAttribute("destination", dest);
			
			return mapping.findForward("goVoting");
		}
		if(dest!=null){
			request.setAttribute("destination", dest);
			errors.add("notExist",new ActionMessage("vote.notExist"));
			return mapping.findForward("gobackDestView");
		}else{
			errors.add("notExist",new ActionMessage("Destination.notExist"));
		}		
		saveErrors(request, errors);
		return mapping.getInputForward();
					
	}


}
